package ouhenry.stage.manage.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ouhenry.stage.common.base.BaseController;
import ouhenry.stage.common.base.Result;
import ouhenry.stage.manage.form.UserForm;
import ouhenry.stage.manage.service.UserService;
import ouhenry.stage.manage.vo.UserLoginVo;

@RestController
@RequestMapping("/user")
@Api(tags = "用户登录")
public class UserLoginController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(UserLoginController.class);

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录", notes = "根据用户名和密码登录")
    @PostMapping("/login")
    public Result<UserLoginVo> login(HttpServletRequest request, @RequestBody UserForm userForm) {
        String ip = getRealIp(request);
        logger.info("ip{},表单登录:{}", ip, userForm);
        Result<UserLoginVo> re = userService.loginVerify(userForm);
        return re;
    }

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @PostMapping("/add")
    public Result<UserLoginVo> add(HttpServletRequest request, @RequestBody UserForm userForm) {
        String ip = getRealIp(request);
        logger.info("ip{},添加用户:{}", ip, userForm);
        Result<UserLoginVo> re = userService.add(userForm);
        return re;
    }

    @ApiOperation(value = "更新用户", notes = "更新用户")
    @PostMapping("/update")
    public Result<UserLoginVo> update(HttpServletRequest request, @RequestBody UserForm userForm) {
        String ip = getRealIp(request);
        logger.info("ip{},更新用户:{}", ip, userForm);
        Result<UserLoginVo> re = userService.update(userForm);
        return re;
    }

}
